﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _019_委托详解_34高级使用
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Student stu1 = new Student() { ID = 1, PenColor = ConsoleColor.Red };
            Student stu2 = new Student() { ID = 2, PenColor = ConsoleColor.Green };
            Student stu3 = new Student() { ID = 3, PenColor = ConsoleColor.Blue };
            Action action1 = new Action(stu1.DoHomeWork);
            Action action2 = new Action(stu2.DoHomeWork);
            Action action3 = new Action(stu3.DoHomeWork);



            //多线程? 

            //直接同步调用
            //stu1.DoHomeWork();
            //stu2.DoHomeWork();
            //stu3.DoHomeWork();

            //多播委托  把方法添加到另一个方法可以实现执行效果
            //action2 += action3;
            //action1 += action2;


            //间接同步调用
            //action1.Invoke();
            //action2.Invoke();
            //action3.Invoke();


            //隐式异步调用
            //action1.BeginInvoke(null, null);
            //action2.BeginInvoke(null, null);
            //action3.BeginInvoke(null, null);

            //显式异步调用
            //Thread threa1 = new Thread(new ThreadStart(stu1.DoHomeWork));
            //Thread threa2 = new Thread(new ThreadStart(stu2.DoHomeWork));
            //Thread threa3 = new Thread(new ThreadStart(stu3.DoHomeWork));
            //threa1.Start();
            //threa2.Start();
            //threa3.Start();

            //另一种调用方式
            Task task1 = new Task(new Action(stu1.DoHomeWork));
            Task task2 = new Task(new Action(stu2.DoHomeWork));
            Task task3 = new Task(new Action(stu3.DoHomeWork));

            task1.Start();
            task2.Start();
            task3.Start();

            for (int i = 0; i < 10; i++)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Main thread {0}", i);
                Thread.Sleep(1000);
            }
        }
    }

    class Student
    {

        public int ID { get; set; }
        public ConsoleColor PenColor { get; set; }

        public void DoHomeWork()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.ForegroundColor = this.PenColor;
                Console.WriteLine("Student {0} doing homework {1} hours", this.ID, i);
                Thread.Sleep(1000);
            }
        }

    }
}
